package com.quanshu.exchange.ui.gui.profile;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.LoginInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 设置昵称
 * Created by xiniu003 on 2017/12/14.
 */

public class AmendUserNameActivity extends BaseActivity {


    @BindView(R.id.amend_after_user_name)
    EditText amendAfterUserName;
    @BindView(R.id.set_name_title)
    BaseTitleBar setNameTitle;

    private String amendAfterUserNameStr;
    private String userNameStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amend_user_name);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        userNameStr = intent.getStringExtra("userNameStr");
        initView();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(setNameTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        setNameTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("设置昵称").setRightText("确定").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {
                updateNickName();
            }
        });
        if (!TextUtils.isEmpty(userNameStr)) {
            amendAfterUserName.setText(userNameStr);
            amendAfterUserName.setSelection(userNameStr.length());
            amendAfterUserName.requestFocus();
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().afterTextChangeEvents(amendAfterUserName,
                textViewAfterTextChangeEvent -> {
                    amendAfterUserNameStr = textViewAfterTextChangeEvent.editable().toString();
                });

    }

    /**
     * 纯数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    //修改昵称
    private void updateNickName() {
        if (TextUtils.isEmpty(amendAfterUserNameStr)) {
            ToastHelper.toast("请输入新昵称");
            return;
        }
        if (isNumeric(amendAfterUserNameStr)) {
            ToastHelper.toast("昵称不能为纯数字");
            return;
        }
        NetClient.getInstance().getShopService(LoginInterface.class)
                .updateNickName(AppUtils.getSignParams(currentUser.getMemberid(), amendAfterUserNameStr),
                        currentUser.getMemberid(), amendAfterUserNameStr)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        Intent intent = new Intent();
                        intent.putExtra("userName", amendAfterUserNameStr);
                        setResult(RESULT_OK, intent);
                        finish();
                        ToastHelper.toast(response.getMsg());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                    }
                });
    }

}
